package com.boot.app.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.app.blog.domain.BlogCategory;
import com.boot.app.blog.mapper.BlogCategoryMapper;
import com.boot.app.blog.service.IBlogCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements IBlogCategoryService {

    private final BlogCategoryMapper blogCategoryMapper;

    @Autowired
    public BlogCategoryServiceImpl(
        BlogCategoryMapper blogCategoryMapper
    ){
        this.blogCategoryMapper = blogCategoryMapper;
    }

    @Transactional
    @Override
    public void removeBlogCategoryById(long id) {
        BlogCategory blogCategory = getById(id);
        if(blogCategory.getParentId() == 0){
            LambdaQueryWrapper<BlogCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BlogCategory::getParentId, id);
            blogCategoryMapper.delete(queryWrapper);
        }
        blogCategoryMapper.deleteById(id);
    }

}
